﻿function CheckInfo() {
    $(".clsWarningMeg").html("");
    SetJobInfo();
    SetAttributeInfo();
    var ret = false;
    ret = volidateConInfo();
    if (ret) {
        if ($("#ContentPlaceHolder2_lkbtnSave").hasClass("aspNetDisabled").toString() != "true") {
                $("#ContentPlaceHolder2_lblMeg").html("数据正在处理,请稍候...");
        }
        else {
            ret = false;
        }
    }
    return ret;
}

function AddContactPerson() {
    ToggleDocument('wrapToggle');
}

function QuitToAddContactPerson() {
    ClearText();
    ToggleDocument('wrapToggle');
}


function ClearText() {
    $("#wrapToggle input[type='text']").val("");
    $("#wrapToggle input:checked").attr("checked", false);
    $(".ImgList").html("");
    var cuid = $("#ContentPlaceHolder1_hdCustomerUID").val();
    $("#wrapToggle input[type='hidden']").val("");
    $("#ContentPlaceHolder1_hdCustomerUID").val(cuid);

    $("#listing-tablebody").html("");

    $("#wrapToggle").find("select").each(function (doc) {
        $("#" + $(this).attr("id") + " option:first").attr("selected", true);
        $(this).change();
    });

    $(".form-table").find("input[type='text']").each(function () {
        $(this).focus();
        $(this).blur();
    });
    $(".form-table .activityWrap").css("display", "none");
    $(".form-table").find(".parnt").each(function () {
        $("#" + $(this).attr("id") + " .activityWrap:first").css("display", "block");
    });
}

function SetJobInfo() {
    var strNeedToDel = "";
    var strNeedToAdd = "";
    if ($(".bankwrap").find("#list_" + $("#ContentPlaceHolder1_hdCPUID").val()).size() == 0) {
        $("#ContentPlaceHolder2_hidJobNatureUID").val("");
        $("#ContentPlaceHolder2_hidMediaRoomRelatedUID").val("");
    }
    var Related = $("#ContentPlaceHolder2_hidJobNatureUID").val();
    var subCatList = Related.split(',');
    //找出需要删除的UID
    if (subCatList.length > 0) {
        $.each(subCatList, function (thiskey, thisval) {
            if ($("#ContentPlaceHolder2_ckbJobNatureList input[value='" + thisval + "']").attr("checked") == false) {
                strNeedToDel += thisval + ",";
            }
        });
        $("#ContentPlaceHolder2_hidDelToJob").val("").val(strNeedToDel);
    }
    //找出需要增加的UID
    $("#ContentPlaceHolder2_ckbJobNatureList input:checked").each(function () {
        var isNeedToAdd = true;
        var checkval = $(this).val();
        $.each(subCatList, function (ToComparekey, tocompareval) {
            if (checkval == tocompareval) {
                isNeedToAdd = false;
                return false;
            }
        });
        if (isNeedToAdd)
        { strNeedToAdd += checkval + ","; }
    });
    $("#ContentPlaceHolder2_hidAddToJob").val("").val(strNeedToAdd);
}

function SetAttributeInfo() {
    var strNeedToDel = "";
    var strNeedToAdd = "";
    if ($(".bankwrap").find("#list_" + $("#ContentPlaceHolder1_hdCPUID").val()).size() == 0) {
        $("#ContentPlaceHolder2_hidAttributeUID").val("");
    }
    var Related = $("#ContentPlaceHolder2_hidAttributeUID").val();
    var subCatList = Related.split(',');
    //找出需要删除的UID
    if (subCatList.length > 0) {
        $.each(subCatList, function (thiskey, thisval) {
            if ($("#ContentPlaceHolder2_ckbAttributeList input[value='" + thisval + "']").attr("checked") == false) {
                strNeedToDel += thisval + ",";
            }
        });
        $("#ContentPlaceHolder2_hidDelToAttribute").val("").val(strNeedToDel);
    }
    //找出需要增加的UID
    $("#ContentPlaceHolder2_ckbAttributeList input:checked").each(function () {
        var isNeedToAdd = true;
        var checkval = $(this).val();
        $.each(subCatList, function (ToComparekey, tocompareval) {
            if (checkval == tocompareval) {
                isNeedToAdd = false;
                return false;
            }
        });
        if (isNeedToAdd)
        { strNeedToAdd += checkval + ","; }
    });
    $("#ContentPlaceHolder2_hidAddToAttribute").val("").val(strNeedToAdd);
}



function Inphase(strType) {
    var strID = "";
    if (strType == "close") {
        CloseDocument('inphasebox');
    }
    else {
        $(".tb_ajax input").each(function () {
            if ($(this).attr("checked") == true) {
                strID += $(this).attr("value") + ",";
            }
        });
        if (strID != "") {
            strID = strID.substring(0, strID.length - 1);
        }
        var AddressUID = $("#ContentPlaceHolder2_hidAddressUID").val();
        if (AddressUID == "") {
            $("#waitMeg").html("暂无数据");
        }
        else {
            $.ajax({
                url: "/ServerAshxFile/UpDateAddressInfo.ashx",
                type: "GET",
                data: { datasource: strID, AddUID: AddressUID, strType: strType },
                datatype: "json",
                beforeSend: function () {
                    if (strType != "close")
                        $("#waitMeg").html("数据正在处理,请稍候...");
                },
                success: function (str) {
                    if (str == "1")
                        $("#waitMeg").html("保存成功.");
                    else
                        $("#waitMeg").html("保存失败.");
                }
            });
        }
    }
}


function GetInfo() {
    var strID = "";
        $(".followtb_ajax input").each(function () {
            if ($(this).attr("checked") == true) {
                strID = $(this).attr("value");
            }
        });

        if (strID == "") {
            $("#followwaitMeg").html("暂无选中项...");
        }
        else {
            var UID = $("#ContentPlaceHolder1_hdCustomerUID").val();
            $.ajax({
                url: "/ServerAshxFile/CPFollowInformation.ashx",
                type: "GET",
                data: { datasource: strID, UID: UID },
                datatype: "json",
                beforeSend: function () { $("#followwaitMeg").html("数据正在处理,请稍候..."); },
                success: function (str) {
                    var json = { "strReturn": "[" + str.toString() + "]" };
                    json = eval(json.strReturn);
                    var length = json.length;
                    if (length > 0) {
                        $("#ContentPlaceHolder2_txtFlat_Eng").val(json[0].Flat_Bldg_Eng);
                        $("#ContentPlaceHolder2_txtDistrict_Eng").val(json[0].District_Eng);
                        $("#ContentPlaceHolder2_txtFlat_Chi").val(json[0].Flat_Bldg_Chi);
                        $("#ContentPlaceHolder2_txtDistrict_Chi").val(json[0].District_Chi);
                        $("#ContentPlaceHolder2_txtStreet_Eng").val(json[0].Street_Rd_Eng);
                        $("#ContentPlaceHolder2_txtCity_Eng").val(json[0].City_Eng);
                        $("#ContentPlaceHolder2_txtStreet_Chi").val(json[0].Street_Rd_Chi);
                        $("#ContentPlaceHolder2_txtCity_Chi").val(json[0].City_Chi);
                        $("#ContentPlaceHolder2_txtArea_Eng").val(json[0].Area_Eng);
                        $("#ContentPlaceHolder2_txtProvince_Eng").val(json[0].Province_Eng);
                        $("#ContentPlaceHolder2_txtArea_Chi").val(json[0].Area_Chi);
                        $("#ContentPlaceHolder2_txtProvince_Chi").val(json[0].Province_Chi);
                        $("#ContentPlaceHolder2_ddlArea option[value=" + json[0].AreaUID + "]").attr("selected", true);
                        $("#ContentPlaceHolder2_ddlArea").change();
                        setTimeout(function () {
                            $("#ContentPlaceHolder2_ddlCountry option[value=" + json[0].CountryUID + "]").attr("selected", true);
                        }, 2000);
                        $("#ContentPlaceHolder2_txtPostCode").val(json[0].PostalCode);
                        CloseDocument('inphasebox');

                    }
                    else {
                        $("#followwaitMeg").html("获取数据失败.");
                    }
                }
            });
        }
}

//填充Table
function GetInphaseAddress(divvalue) {
    if (divvalue == "inphasebox") {
        CloseDocument('inphasebox');
    }
    else {
        CloseDocument('inphasebox');
    }
    var code = $("#ContentPlaceHolder1_hdCustomerUID").val();
    ToggleDocument(divvalue);
    if ($("#ContentPlaceHolder2_hidAllowToOption").attr("value") == "false") {
        $(".tb_ajax").html("<tr><td height=\"40\"><span class=\"clsWarningMeg\">暂无数据</span></td></tr>");
        $(".followtb_ajax").html("<tr><td height=\"40\"><span class=\"clsWarningMeg\">暂无数据</span></td></tr>");
    }
    else {
        $.ajax({
            url: "/ServerAshxFile/GetCPAndBranchOffice.ashx?uid=" + code,
            beforeSend: function () {
                if (divvalue == "inphasebox") {
                    $(".followtb_ajax").html("<tr><td height=\"40\"><span class=\"clsWarningMeg\">正在获取数据,请稍候...</span></td></tr>");
                }
                else {
                    $(".tb_ajax").html("<tr><td height=\"40\"><span class=\"clsWarningMeg\">正在获取数据,请稍候...</span></td></tr>");
                }
            },
            success: function (str) {
                var json = { "retValue": "[" + str.toString() + "]" };
                json = eval(json.retValue);
                var strToTable = "";
                var len = json.length;
                if (len > 0) {
                    strToTable += "<tr><td height=\"20\"></td></tr>";
                    for (var i = 0; i < len; i++) {
                        if (divvalue == "inphasebox") {
                            strToTable += "<tr><td><input type=\"radio\" name=\"cbkbox\" value=\"" + json[i].uid + "_" + json[i].type + "\" id=\"follow" + json[i].uid + "_" + json[i].type + "\" /><label for=\"follow" + +json[i].uid + "_" + json[i].type + "\">" + json[i].name + "</label></td></tr>";
                        }
                        else {
                            strToTable += "<tr><td><input type=\"checkbox\" value=\"" + json[i].uid + "_" + json[i].type + "\" id=\"" + json[i].uid + "_" + json[i].type + "\" /><label for=\"" + +json[i].uid + "_" + json[i].type + "\">" + json[i].name + "</label></td></tr>";
                        }
                    }
                    if (divvalue == "inphasebox") {
                        strToTable += "<tr><td height=\"20\" id=\"followwaitMeg\" class=\"clsWarningMeg\"></td></tr>";
                    }
                    else {
                        strToTable += "<tr><td height=\"20\" id=\"waitMeg\" class=\"clsWarningMeg\"></td></tr>";
                    }
                }
                else {
                    strToTable += "<tr><td height=\"40\"><span class=\"clsWarningMeg\">暂无数据</span></td></tr>";
                }
                if (divvalue == "inphasebox") {
                    $(".followtb_ajax").html(strToTable);
                }
                else {
                    $(".tb_ajax").html(strToTable);
                }
            }
        });
    }
}
//填充Table结束


function EditCommiteeMember(type, uid) {
    $("#committeeMeg").html("");
    undo_disp_confirms('commit');
    $("#hdcommitteeMemberID").attr("value", "");
    $("#ContentPlaceHolder2_ddlCommType option:first").attr("selected", true);
    $("#ContentPlaceHolder2_ddlCommPosition option:first").attr("selected", true);
    $("#ContentPlaceHolder2_txtStartDate").val("");
    $("#ContentPlaceHolder2_txtEndDate").val("");
    $("#ContentPlaceHolder2_txtYear").val("");
    $("#ContentPlaceHolder2_txtRmark").val("");
    if (type == "get") {
        $.ajax({
            url: "/ServerAshxFile/CommitteeMember.ashx",
            type: "GET",
            data: { OptionType: type, CMUID: uid },
            beforeSend: function () { $("#committeeMeg").html("正在获取数据,请稍候..."); },
            success: function (str) {
                var json = { "retValue": "[" + str.toString() + "]" };
                try {
                    json = eval(json.retValue);
                    $("#hdcommitteeMemberID").attr("value", uid);
                    $("#ContentPlaceHolder2_ddlCommType option[value='" + json[0].TypeValue + "']").attr("selected", true);
                    $("#ContentPlaceHolder2_ddlCommPosition option[value='" + json[0].Position + "']").attr("selected", true);
                    $("#ContentPlaceHolder2_txtStartDate").val(json[0].txtStartDate);
                    $("#ContentPlaceHolder2_txtEndDate").val(json[0].txtEndDate);
                    $("#ContentPlaceHolder2_txtYear").val(json[0].txtYear);
                    $("#ContentPlaceHolder2_txtRmark").val(json[0].txtRmark);
                    $("#committeeMeg").html("");
                }
                catch (Error) {
                    $("#committeeMeg").html(str);
                };
            }
        });
    }
}

function SaveCommiteeMember() {
    var committeeUID = $("#hdcommitteeMemberID").val();
    var type = "add";
    if (committeeUID != "") {
        type = "update";
    }
    $.ajax({
        url: "/ServerAshxFile/CommitteeMember.ashx",
        type: "GET",
        data: {
            OptionType: type,
            CMUID: committeeUID,
            CustomerUID: $("#ContentPlaceHolder1_hdCustomerUID").val(),
            cid: $("#ContentPlaceHolder1_hdCPUID").val(),
            TypeValue: $("#ContentPlaceHolder2_ddlCommType").val(),
            Position: $("#ContentPlaceHolder2_ddlCommPosition").val(),
            txtStartDate: $("#ContentPlaceHolder2_txtStartDate").val(),
            txtEndDate: $("#ContentPlaceHolder2_txtEndDate").val(),
            txtYear: $("#ContentPlaceHolder2_txtYear").val(),
            txtRmark: $("#ContentPlaceHolder2_txtRmark").val()
        },
        beforeSend: function () { $("#committeeMeg").html("正在处理数据,请稍候..."); },
        success: function (str) {
            $("#committeeMeg").html(str);
            $("#ContentPlaceHolder2_btnRefreshCommitee").click();
        }
    });
}
function CloseCommiteeMember() {
    $(".close_out").click();
}

function DeleteCommiteeMember(commuid) {
    $("#hdcommitteeMemberID").val("");
    $("#hdcommitteeMemberID").attr("value", commuid);
    delete_disp_confirms('DeleteBox');
}

function DeleteCust_CP(uid) {
    if (!arguments[0])
        uid = args1;
    $.ajax({
        url: "/ServerAshxFile/DeleteContactPerson.ashx",
        data: { UID: uid },
        beforeSend: function () { },
        success: function (str) {
            var json = { "retValue": "[" + str.toString() + "]" };
            json = eval(json.retValue);
            if (json[0].Issucess == "1") {
                $("#ContentPlaceHolder2_btnRefreshCPList").click();
            }
            //输出信息
        }
    });
}


function DeleteCommit() 
{
    //var committeeUID = $("#hdcommitteeMemberID").val();
    var committeeUID = args1;
    $("#sureMeg").html("");
    $.ajax({
        url: "/ServerAshxFile/CommitteeMember.ashx",
        type: "GET",
        data: {
            OptionType: "del",
            CMUID: committeeUID
        },
        beforeSend: function () {
//            $("#sureMeg").html("正在处理数据,请稍候...");
        },
        error: function () {
//            $("#sureMeg").html("删除失败.");
        },
        success: function (str) {
//            $("#sureMeg").html(str);
            $("#ContentPlaceHolder2_btnRefreshCommitee").click();
        }
    });

}



//图片相关-----------
function AddImg(type) {
    ToggleDocument('smallImgList2');
    $("#id_message").html("");
}
function EditImgRelated(thisval) {
    ToggleDocument('smallImgList2');
}
// 关联操作
function ToRelated(thisval) {
    var fileName = $(thisval).find("img").attr("alt");
    var ImgUrl = "/media/upload/contactpersonphoto/" + fileName + "_150_150.jpg";
    var imguid = $(thisval).find("img").attr("class");
    if ($(".ImgList").find("." + imguid).size() == 0) {
        var AddedHtml = "<li class=\"" + imguid + "\"><p class=\"p2\"><img src=\"" + ImgUrl + "\" /></p><div class=\"p4\"><span class=\"showbig\" onclick=\"EditImgRelated(this)\">Edit</span> | <span class=\"closeBtnbig\" onclick=\"DeleteRelated(this)\">Delete</span></div></li>";
        $(".ImgList").html(AddedHtml);
        $("#ContentPlaceHolder2_hidMediaRoomUID").val(imguid);
    }
    else {
        alert("图片：" + fileName + "\n已被添加到列表中.");
    }
}

function DeleteRelated(thisval) {
    $(thisval).parents("li").remove();
    $("#ContentPlaceHolder2_hidMediaRoomUID").val("");
}